package cn.swing.learning;


import java.awt.Color;
import java.awt.GridLayout;

import javax.swing.*;
import javax.swing.border.Border;

public class MineField extends JPanel {

    private MineFrame mineFrame;

    private MineLabel[][] mineLabels;

    private MineLabelMouseListener mineLabelMouseListener;

    public MineField(MineFrame mineFrame) {
        this.mineFrame = mineFrame;
        Border borderIn = BorderFactory.createLoweredBevelBorder();
        Border borderOut = BorderFactory.createEmptyBorder(5, 5, 5, 5);
        Border border = BorderFactory.createCompoundBorder(borderOut, borderIn);
        this.setBorder(border);
        this.setBackground(Color.LIGHT_GRAY);
        mineLabelMouseListener = new MineLabelMouseListener(mineFrame);
        this.setLayout(new GridLayout(Tools.rows, Tools.cols));
        mineLabels = new MineLabel[Tools.rows][Tools.cols];
        for (int i = 0; i < mineLabels.length; i++) {
            for (int j = 0; j < mineLabels[i].length; j++) {
                mineLabels[i][j] = new MineLabel();
                mineLabels[i][j].setCol(j);
                mineLabels[i][j].setRow(i);
                mineLabels[i][j].addMouseListener(mineLabelMouseListener);
                this.add(mineLabels[i][j]);
            }
        }

    }


    public void buildMine() {
        for (int i = 0; i < Tools.totalMines; i++) {
            int row = (int) (Math.random() * Tools.rows);
            int col = (int) (Math.random() * Tools.cols);
            mineLabels[row][col].setMine(true);
        }
        countArroundMines();
    }

    public void countArroundMines() {
        for (int i = 0; i < mineLabels.length; i++) {
            for (int j = 0; j < mineLabels[i].length; j++) {

                int arroundCount = 0;
                for (int k = Math.max(i - 1, 0); k <= Math.min(i + 1, Tools.rows - 1); k++) {
                    for (int l = Math.max(j - 1, 0); l <= Math.min(j + 1, Tools.cols - 1); l++) {
                        if (mineLabels[k][l].isMine()) {
                            arroundCount++;
                        }
                    }
                }
                mineLabels[i][j].setArroundMines(arroundCount);

            }
        }
    }

    public void openAll() {
        for (int i = 0; i < mineLabels.length; i++) {
            for (int j = 0; j < mineLabels[i].length; j++) {
                if (mineLabels[i][j].isMine()) {
                    mineLabels[i][j].setIcon(Tools.mine);
                } else {
                    mineLabels[i][j].setIcon(Tools.arrounMines[mineLabels[i][j].getArroundMines()]);
                }
            }
        }
    }

    public void win() {
        int blanks = 0;

        for (int i = 0; i < mineLabels.length; i++) {
            for (int j = 0; j < mineLabels[i].length; j++) {
                if (!mineLabels[i][j].isBlank()) {
                    blanks++;
                }
            }
        }

        if (blanks == Tools.totalMines) {
            removeListener();
//            JOptionPane.showMessageDialog(null, "你赢了");
            new IntegralDialog();
        }

    }

    public void fail(MineLabel mineLabel) {
        openAll();
        removeListener();
        mineLabel.setIcon(Tools.mines[2]);

        JOptionPane.showMessageDialog(null, "你输了");
    }

    public void openMine(MineLabel mineLabel) {

        if (!mineLabel.isBlank()) {

            if (mineLabel.isMine()) {
                fail(mineLabel);
            } else {
                mineLabel.setIcon(Tools.arrounMines[mineLabel.getArroundMines()]);
                mineLabel.setBlank(true);
                if (mineLabel.getArroundMines() == 0) {
                    int i = mineLabel.getRow();
                    int j = mineLabel.getCol();
                    for (int k = Math.max(i - 1, 0); k <= Math.min(i + 1, Tools.rows - 1); k++) {
                        for (int l = Math.max(j - 1, 0); l <= Math.min(j + 1, Tools.cols - 1); l++) {
                            openMine(mineLabels[k][l]);
                        }
                    }
                }
            }

        }
    }

    public void removeListener() {
        for (int i = 0; i < mineLabels.length; i++) {
            for (int j = 0; j < mineLabels[i].length; j++) {
                mineLabels[i][j].removeMouseListener(mineLabelMouseListener);
            }
        }
    }
}
